<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  var arr = function (nums) {
    let i = 0, num = nums.length;
    // 循环执行 nums 的长度 - 2 的数量
    while (i < num) {
      if (nums[i] == 0) {
        nums.splice(i++, 1);
        nums.unshift(0);
      } else if (nums[i] == 2) {
        nums.splice(i, 1);
        nums.push(2)
        num--;
      } else i++
    }
    return nums;
  };
  console.log(arr(nums = [2, 0, 2, 1, 1, 0]));
</script>

</html>